package com.car.action.admin;

import com.car.common.model.UserAlbums;
import com.car.intercept.AdminInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.utils.PicUtil;
import com.utils.StringUtil;

@Before(AdminInterceptor.class)
public class PicController extends Controller{

	public void index() {
		String sql = "select a.user_id,a.id,a.status,a.create_time,u.nick_name,u.user_mobile,a.img,CASE a.status  WHEN '1' THEN '审核成功' WHEN '2' THEN '待审核'  WHEN '3' THEN '审核失败' ELSE '其他' END as status_name ";
		
		sql += ", FROM_UNIXTIME(a.create_time,'%Y-%m-%d %H:%i') as create_time_string ";
		
		String andSql = " from user_albums a left join users u on u.id = a.user_id where a.STATUS != 1 ";
		andSql += " order by field(a.STATUS, 2, 3),a.id DESC ";
		
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        } 
        Page<UserAlbums> albumsPage = UserAlbums.dao.paginate(page, 15, sql,andSql);
        for(UserAlbums one : albumsPage.getList()) {
        	one.setImg(PicUtil.QINIU_DIR + one.getImg() );
        }
        setAttr("page", albumsPage);
        render("index.html");
	}
	
	public void check_pic() {
		String[] ids = getPara("ids").split(",");
 		for(String id :ids) {
 			Db.update("update user_albums set status = 1 where id = "  + id);
		}
		renderText("操作成功!");
	}
	
	public void del() {
		String[] ids = getPara("ids").split(",");
 		for(String id :ids) {
			UserAlbums oneAlbums = UserAlbums.dao.findById(id);
			if (oneAlbums != null) {
				PicUtil tool = new PicUtil();
				tool.delete(oneAlbums.getImg());
				Db.delete("delete from user_albums where id =  " + id);
			}
		}
		renderText("操作成功!");
		
	}
}
